﻿using DLL.NET50.DB.MS.Entity;
using Microsoft.EntityFrameworkCore;
using System;

namespace DLL.NET50.DB.MS.DrugBackup.Context
{
    public class CMSSaveDrugsContext : DbContext
    {
        //public CMSSaveDrugsContext()
        //{
        //}

        //public CMSSaveDrugsContext(DbContextOptions<CMSSaveDrugsContext> options)
        //    : base(options)
        //{
        //}

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseMySQL("Server=localhost;Database=mssavedrugs;User ID=root;Password=123456;Connection Timeout=120;SslMode=None");
                //optionsBuilder.AutomaticMigrationDataLossAllowed = true;
            }
        }

        public virtual DbSet<adi_FlushGroup> adi_flushGroups { get; set; }
        public virtual DbSet<adi_dic_drug> adi_dic_drug { get; set; }
        public virtual DbSet<adi_FlushParam> adi_flushParams { get; set; }
        public virtual DbSet<adi_PumpBackParam> adi_PumpBackParams { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //配置药品，和冲刷参数 的一对多关系
            //配置药品，和冲刷参数 的一对多关系
            modelBuilder.Entity<adi_FlushParam>()
                .HasOne(d => d.flushGroup)
                .WithMany(fp => fp.flush_Params)
                .HasForeignKey(f => f.DrugFlushGroupCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_PumpBackParam>()
                .HasOne(d => d.flushGroup)
                .WithMany(fp => fp.pumpBack_Params)
                .HasForeignKey(f => f.DrugFlushGroupCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_FlushGroup>()
                .HasOne(d => d.drug)
                .WithMany(fg => fg.flush_Groups)
                .HasForeignKey(fg => fg.Drug_UniqueCode)
                .OnDelete(DeleteBehavior.Cascade);

            modelBuilder.Entity<adi_dic_drug>()
                .HasKey(d => d.Drug_UniqueCode);
            base.OnModelCreating(modelBuilder);
        }
    }
}